package com.example.xm.ui.home.activity.youhome_fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.xm.R;
import com.example.xm.adapter.HomeAdapter.YouHomeExerciseAdapter;
import com.example.xm.base.BaseAdapter;
import com.example.xm.bean.HomeBean.HomeYouHomeExerciseBean;
import com.example.xm.interfaces.ILoadData;
import com.example.xm.ui.home.activity.youhome_fragments.exercise_activity.Exercise_rlv_Activity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ExerciseFragment extends Fragment{
    @BindView(R.id.rlv_exercise)
    RecyclerView rlvExercise;
    private int type;
    private HomeYouHomeExerciseBean data;
    private ArrayList<HomeYouHomeExerciseBean.DataBean.ActivitysBean> list;
    private YouHomeExerciseAdapter youHomeExerciseAdapter;
    private ILoadData loadData;

    public ExerciseFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_youhome_exercise, null);
        ButterKnife.bind(this, inflate);
        return inflate;

    }

    /**
     * 获取ExerciseFragment的实例对象
     *
     * @param type
     * @return
     */
    public static ExerciseFragment ExerciseFragment(int type, ILoadData loadData) {
        ExerciseFragment exerciseFragment = new ExerciseFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        exerciseFragment.loadData = loadData;
        exerciseFragment.setArguments(bundle);
        return exerciseFragment;
    }

//    @Override
//    public void setUserVisibleHint(boolean isVisibleToUser) {
//        super.setUserVisibleHint(isVisibleToUser);
//        if (isVisibleToUser && rlvExercise != null) {
//            if (data != null) {
//                if (loadData != null) {
//                    loadData.loadData(type);
//                }
//            }
//        }
//    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        list = new ArrayList<>();
        youHomeExerciseAdapter = new YouHomeExerciseAdapter(getActivity(), list);
        rlvExercise.setLayoutManager(new LinearLayoutManager(getActivity()));
        rlvExercise.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayout.VERTICAL));
        rlvExercise.setAdapter(youHomeExerciseAdapter);
        Bundle arguments = getArguments();
        if (arguments.containsKey("type")) {
            type = arguments.getInt("type");
        }
        if (loadData != null) {
            loadData.loadData(type);//设置当前fragment的下标
        }
        initListener();
    }

    private void initListener() {
        youHomeExerciseAdapter.setOnClick(new BaseAdapter.onClick() {
            @Override
            public void click(int pos) {
                if(pos < data.getData().getActivitys().size()){
                    Intent intent = new Intent(getContext(), Exercise_rlv_Activity.class);
                    intent.putExtra("detail",data.getData().getActivitys().get(pos));
                    startActivity(intent);
                }
            }
        });
    }

    /**
     * 初始化数据
     *
     * @param bean
     */
    public void initData(HomeYouHomeExerciseBean bean) {
        data = bean;
        list.addAll(bean.getData().getActivitys());
        upData();
    }

    /**
     * 刷新数据
     */
    private void upData() {
        youHomeExerciseAdapter.notifyDataSetChanged();
    }
}
